package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.ACategory;
import com.ruoyi.system.domain.AProduct;
import com.ruoyi.system.domain.ASetmealDetail;
import com.ruoyi.system.domain.dto.SetMealDato;
import com.ruoyi.system.service.ASetmealDetailService;
import com.ruoyi.system.service.IAProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ASetmeal;
import com.ruoyi.system.service.IASetmealService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 套餐管理Controller
 * 
 * @author 王家宁
 * @date 2024-08-18
 */
@Controller
@RequestMapping("/system/setmeal")
public class ASetmealController extends BaseController
{
    private String prefix = "system/setmeal";

    @Autowired
    private IASetmealService aSetmealService;
    @Autowired
    private ASetmealDetailService aSetmealDetailService;
    @Autowired
    private IAProductService productService;

    @GetMapping()
    public String setmeal()
    {
        return prefix + "/setmeal";
    }

    /**
     * 查询套餐管理列表
     */
//    @RequiresPermissions("system:setmeal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ASetmeal aSetmeal)
    {
        startPage();
        List<ASetmeal> list = aSetmealService.selectASetmealList(aSetmeal);
        return getDataTable(list);
    }

    /**
     * 导出套餐管理列表
     */
    @RequiresPermissions("system:setmeal:export")
    @Log(title = "套餐管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ASetmeal aSetmeal)
    {
        List<ASetmeal> list = aSetmealService.selectASetmealList(aSetmeal);
        ExcelUtil<ASetmeal> util = new ExcelUtil<ASetmeal>(ASetmeal.class);
        return util.exportExcel(list, "套餐管理数据");
    }

    /**
     * 新增套餐管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存套餐管理
     */
    @RequiresPermissions("system:setmeal:add")
    @Log(title = "套餐管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SetMealDato setMealDato)
    {
        ASetmeal setmeal = new ASetmeal();
        BeanUtils.copyProperties(setMealDato,setmeal);
        aSetmealService.insertASetmeal(setmeal);
        String[] split = setMealDato.getProductId().split(",");
        List<String> productIds = Arrays.asList(split);
        productIds.forEach(productId -> {
            AProduct product = productService.getOne(Wrappers.<AProduct>lambdaQuery()
                    .select(AProduct::getProductName,AProduct::getProductPrice)
                    .eq(AProduct::getId, Long.valueOf(productId))
            );
            ASetmealDetail aSetmealDetail = new ASetmealDetail();
            aSetmealDetail.setProductId(Long.valueOf(productId));
            aSetmealDetail.setSetmealId(setmeal.getId());
            aSetmealDetail.setProductName(product.getProductName());
            aSetmealDetail.setProductPrice(product.getProductPrice());
            aSetmealDetailService.insertASetmealDeatil(aSetmealDetail);
        });
        return toAjax(1);
    }

    /**
     * 修改套餐管理
     */
    @RequiresPermissions("system:setmeal:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ASetmeal aSetmeal = aSetmealService.selectASetmealById(id);
        mmap.put("aSetmeal", aSetmeal);
        return prefix + "/edit";
    }

    /**
     * 修改保存套餐管理
     */
    @RequiresPermissions("system:setmeal:edit")
    @Log(title = "套餐管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ASetmeal aSetmeal)
    {
        return toAjax(aSetmealService.updateASetmeal(aSetmeal));
    }

    /**
     * 删除套餐管理
     */
    @RequiresPermissions("system:setmeal:remove")
    @Log(title = "套餐管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(aSetmealService.deleteASetmealByIds(ids));
    }
}
